import globals.DrawPadSize;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class Painter {
	
	private static JFrame paintFrame;
	private static DrawPad paintPad;
	private static ToolBox paintTool;
	
	public static void main(String[] args){
		
		// Frame setting
		paintFrame = new JFrame("Paint in java");
		// Area for painting
		paintPad = new DrawPad();
		// Area of tool buttons
		paintTool = new ToolBox(paintPad);
		// Area for menu
		final Menu menuBar = new Menu(paintPad);
		menuBar.setMenu();
				
		// Group layout settings
		GroupLayout layout = new GroupLayout(paintFrame.getContentPane());
		paintFrame.setLayout(layout);
		
		// Auto gaps
		//layout.setAutoCreateGaps(true);
		//layout.setAutoCreateContainerGaps(true);
		// Horizontal Group
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addComponent(paintTool.getToolPanel())
				.addComponent(paintPad, GroupLayout.PREFERRED_SIZE, DrawPadSize.getWidth(), GroupLayout.PREFERRED_SIZE)
		);		
		// Vertical Group
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(paintTool.getToolPanel())
				.addComponent(paintPad, GroupLayout.PREFERRED_SIZE, DrawPadSize.getHeight(), GroupLayout.PREFERRED_SIZE)
		);		
		
		// Frame initial parameters
		//paintFrame.setSize(800, 600);
		//Fullscreen
		paintFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		//Let user be informed about losing data, allow cancel button to NOT close program
		paintFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		paintFrame.setVisible(true);
		paintFrame.setJMenuBar(menuBar.getMenuBar());
		
		
		paintFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				int result = JOptionPane.showConfirmDialog(null,"Do you want to save your progress?", "Save progress?", JOptionPane.YES_NO_CANCEL_OPTION);
	    	    switch(result){
	    	        case JOptionPane.YES_OPTION:
	    			    paintPad.saveImage();
	    	        case JOptionPane.NO_OPTION:
	    	            System.exit(0);
	    	            break;
	    	        case JOptionPane.CANCEL_OPTION:
	    	            break;
	    	        }
			    }
			});
	} 
	
	public static void setLayout(){
		// Group layout settings
		GroupLayout layout = new GroupLayout(paintFrame.getContentPane());
		paintFrame.setLayout(layout);
		// Auto gaps
		//layout.setAutoCreateGaps(true);
		//layout.setAutoCreateContainerGaps(true);
		
		// Horizontal Group
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addComponent(paintTool.getToolPanel())
				.addComponent(paintPad, GroupLayout.PREFERRED_SIZE, DrawPadSize.getWidth(), GroupLayout.PREFERRED_SIZE)
		);		
		// Vertical Group
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(paintTool.getToolPanel())
				.addComponent(paintPad, GroupLayout.PREFERRED_SIZE, DrawPadSize.getHeight(), GroupLayout.PREFERRED_SIZE)
		);
	}
}
